package com.yhb.controller;

import com.yhb.controller.vo.ArrayWrapperVo;
import com.yhb.model.Address;
import com.yhb.model.User;
import com.yhb.service.AddressService;
import me.edzh.spring.bootstrap.annotations.CurrentUser;
import me.edzh.spring.bootstrap.annotations.NeedAuth;
import me.edzh.spring.bootstrap.model.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @NeedAuth
    @GetMapping("/")
    public Response<ArrayWrapperVo<Address>> myAddress(@CurrentUser User user) {
        return Response.ok(ArrayWrapperVo.<Address>builder()
                .fuckingList(addressService.userAddress(user.getId()))
                .build());
    }

    @NeedAuth
    @PostMapping("/modify")
    public Response modifyAddress(@RequestBody Address address, @CurrentUser User user) {
        address.setUserId(user.getId());
        addressService.modify(address);
        return Response.ok();
    }

    @NeedAuth
    @GetMapping("/{id}/delete")
    public Response deleteAddress(@PathVariable("id") String addressId, @CurrentUser User user) {
        addressService.delete(Address.builder()
                .id(addressId)
                .userId(user.getId())
                .build());

        return Response.ok();
    }

    @NeedAuth
    @PostMapping("/add")
    public Response<Address> addAddress(@RequestBody Address address, @CurrentUser User user) {
        address.setUserId(user.getId());
        addressService.add(address);
        return Response.ok(address);
    }
}
